<template>
  <div>
    <el-dialog title="原料单位" class="noselect"
               v-dialogdrag=" { target:'.el-dialog__header',container:'.el-dialog',border:'.el-dialog__wrapper'}"
               :close-on-click-modal="false" :visible.sync="showDialog" :before-close="handleClose">
      <div style="margin: 20px;"></div>
      <el-form :model="modelForm" ref="modelForm" label-width="100px" class="demo-ruleForm">
       <!-- <el-form-item label=" " prop="no" :rules="[{ required: true, message: '编号不能为空且不能重复', trigger: 'blur' }]">
          <el-input placeholder="请输入编号" v-model="modelForm.no" size="medium">
            <template slot="prepend">
              <el-tooltip placement="top" content="请填写编号">
                <el-button tabIndex="-1">编号</el-button>
              </el-tooltip>
            </template>
          </el-input>
        </el-form-item>-->
        <el-form-item label=" " prop="name" :rules="[{ required: true, message: '名称不能为空且不能重复', trigger: 'blur' }]">
          <el-input placeholder="请输入名称" v-model="modelForm.name" size="medium">
            <template slot="prepend">
              <el-tooltip placement="top" content="请填写名称">
                <el-button tabIndex="-1">名称</el-button>
              </el-tooltip>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label=" " prop="nameEn" :rules="[]">
          <el-input placeholder="请输入英文名称" v-model="modelForm.nameEn" size="medium">
            <template slot="prepend">
              <el-tooltip placement="top" content="请填写英文名称">
                <el-button tabIndex="-1">英文名称</el-button>
              </el-tooltip>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label=" " prop="remark" :rules="[]">
          <el-input placeholder="请输入备注" v-model="modelForm.remark" size="medium">
            <template slot="prepend">
              <el-tooltip placement="top" content="请填写备注">
                <el-button tabIndex="-1">备注</el-button>
              </el-tooltip>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label=" " prop="state" :rules="[]">
          <div class="el-input el-input-group el-input-group--prepend addprepend el-input--medium">
            <div class="el-input-group__prepend addPrependText-medium" tabIndex="-1">
              <span>状态</span>
            </div>
            <el-radio-group class="radio-group w-100p text-left" v-model="modelForm.state">
              <el-radio v-for="item in states" :label="item.key" :value="item.key" :key="item.key">
                {{item.text}}
              </el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
      </el-form>
      <div class="foot-bar-button">
        <el-button type="primary" @click="submitForm('modelForm')" size="medium">提交</el-button>
        <el-button @click="resetForm('modelForm')" size="medium">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  //注意路径
  export default {
    props: ['isShow', 'id'],  //参数
    data() {
      return {
        modelForm: {},
        showDialog: false,
        activityId: "",
        isSave: true,
        states: []
      };
    },
    watch: {
      isShow: function (newValue, oldValue) {
        this.showDialog = newValue;
        if (newValue) {
          if (this.id != '') {
            this.activityId = this.id;
            this.isSave = false;

            this.$axios.get("materials/materialUnits/" + this.activityId + "/update").then(data => {
              this.modelForm = data.content.materialUnit;
              this.states = data.content.states;
            });
          } else {
            this.isSave = true;
            this.$axios.get("materials/materialUnits/create").then(data => {
              this.modelForm = data.content.materialUnit;
              this.states = data.content.states;
            });
          }
        }
      }
    },
    created() {
      window.this = this;
    },
    methods: {
      setPy() {
        this.modelForm.pinyin = window.getPy(this.modelForm.name);
        this.modelForm.spell = window.getIndexPy(this.modelForm.name);
      },
      goBack() {
        this.router.go(-1);
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.isSave) {
              this.$axios.post("materials/materialUnits/create", this.modelForm).then(data => {
                if (data.status == 200) {
                  this.$message({
                    message: data.message,
                    type: 'success'
                  });
                  this.$emit('addSuccess');
                } else {
                  this.$message.error(data.message);
                }
              })
            } else {
              this.modelForm._method = "PUT";
              this.$axios.put("materials/materialUnits/" + this.activityId + "/update", this.modelForm).then(data => {
                if (data.status == 200) {
                  this.$message({
                    message: data.message,
                    type: 'success'
                  });
                  this.$emit('addSuccess');
                } else {
                  this.$message.error(data.message);
                }
              })
            }
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$emit('close');
        this.showDialog = false;
      },
      handleClose(done) {
        this.$emit('close');
        done();
      },
      isNum(item, key) {
        var r = /^\+?[1-9][0-9]*$/;
        if (!r.test(item[key])) {
          item[key] = "0.00"
        } else {
          item[key] = item[key]
        }
      },
      isDouble(item, key) {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(item[key])) {
          item[key] = "0.00"
        } else {
          item[key] = item[key]
        }
      }
    }
  }
</script>
<style scoped>

</style>
